/**
 * Copyright (C), 2015-2020, 云帐房
 * FileName: DataSourceServiceImpl
 * Author:   cjz
 * Date:     2020/5/12 下午5:45
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.yzf.wisecoder.service.impl;

import com.yzf.cframework.common.db.DBHelper;
import com.yzf.cframework.common.http.HttpUtils;

import com.yzf.cframework.common.json.JsonUtils;
import com.yzf.wisecoder.remotedata.*;
import com.yzf.wisecoder.service.DataSourceService;
import com.yzf.wisecoder.wisecodercommon.enums.DataSourceTypeEnum;
import com.yzf.wisecoder.wisecoderservice.service.ViewDatasourceService;
import com.yzf.wisecoder.wisecoderservice.service.ViewDbService;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewDatasourceEntity;
import com.yzf.wisecoder.wisecoderstorage.mysql.entity.ViewDbEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : DataSourceServiceImpl
 * @Description : 组件数据源
 * @Author : cjz
 * @Date: 2020-05-12 17:45
 */
@Service
public class DataSourceServiceImpl implements DataSourceService {

    private static final Logger logger = LoggerFactory.getLogger(DataSourceServiceImpl.class);
    @Autowired
    private ViewDatasourceService viewDatasourceService;
    @Autowired
    private ViewDbService viewDbService;

    @Override
    public Object gerRemoteById(Long dataSourceId) throws Exception {
        Object result = null;
        ViewDatasourceEntity viewDatasource = viewDatasourceService.getById(dataSourceId);

        if (Objects.isNull(viewDatasource)){
            logger.error("数据不存在:{}",dataSourceId);
            throw new Exception("数据不存在");
        }

        ViewDbEntity viewDbEntity = viewDbService.getById(viewDatasource.getDbId());
        if (Objects.isNull(viewDbEntity)){
            logger.error("数据源不存在:{}",viewDatasource.getDbId());
            throw new Exception("数据源不存在");
        }

        RemoteInfo remoteInfo = new RemoteInfo();
        remoteInfo.setUrl(viewDbEntity.getDbUrl());
        remoteInfo.setUserName(viewDbEntity.getDbUsername());
        remoteInfo.setPassword(viewDbEntity.getDbPassword());
        remoteInfo.setExecute(viewDatasource.getExecuteText());
        remoteInfo.setDbType(viewDbEntity.getDbType());

        return doExecute(remoteInfo);
    }

    @Override
    public Object gerRemoteByDbIdAndExecute(Long dbId, String execute) throws Exception {

        ViewDbEntity viewDbEntity = viewDbService.getById(dbId);
        if (Objects.isNull(viewDbEntity)){
            logger.error("数据源不存在:{}",dbId);
            throw new Exception("数据源不存在");
        }

        RemoteInfo remoteInfo = new RemoteInfo();
        remoteInfo.setUrl(viewDbEntity.getDbUrl());
        remoteInfo.setUserName(viewDbEntity.getDbUsername());
        remoteInfo.setPassword(viewDbEntity.getDbPassword());
        remoteInfo.setExecute(execute);
        remoteInfo.setDbType(viewDbEntity.getDbType());

        return doExecute(remoteInfo);
    }

    private Object doExecute(RemoteInfo remoteInfo) {

        DataSourceTypeEnum typeEnum = DataSourceTypeEnum.valueOf(Integer.parseInt(remoteInfo.getDbType()));
        RemoteDataSource remoteDataSource = null;
        switch (typeEnum){
            case HTTP:
                remoteDataSource = new HttpRemoteDataSource(remoteInfo);
                break;
            case SQL:
                remoteDataSource = new SqlRemoteDataSource(remoteInfo);
                break;
            case ELASTICSEARCH:
                remoteDataSource = new ElasticSearchRemoteDataSource(remoteInfo);
                break;
        }

        return remoteDataSource.execute().result();
    }

}
